var CognosReport = (function () {

    var jsonData, chart, columns, rows, values, corner, xAxis, series, rowCount, columnCount, chartType, title;
    var getChart = function () {
        var types = ['crosstab', 'metadata']
        for (x in types) {
            var c = jsonData.dataset[types[x]]
            //	        console.log(c)
            if (c !== undefined) {
                return {
                    type: types[x],
                    node: c
                };
            }
        }
    }

    var getColumns = function () {
        var result = [];
        var i;
        if (chart.node.columns.colEdge !== undefined) {
            for (i = 0; i < chart.node.columns.colEdge.length; i++) {
                //            console.log($scope.chart().node.columns.colEdge[i])
                result.push(chart.node.columns.colEdge[i].caption)
            }
            columnCount = result.length;
        } else {
            columnCount = 0;
        }
        return result;
    }


    var getRows = function () {
        var result = [];
        var i;
        //                console.log(chart.node.rows.rowEdge)
        if (chart.node.rows.rowEdge !== undefined) {
            for (i = 0; i < chart.node.rows.rowEdge.length; i++) {
                result.push(chart.node.rows.rowEdge[i].caption)
            }
            rowCount = result.length;
        } else {
            rowCount = 1;
        }
        return result;
    }


    var getValues = function () {
        var result = [];
        var i;
        for (i = 0; i < chart.node.values.value.length; i++) {
            var dataStr = chart.node.values.value[i].__text
            result.push(parseFloat(dataStr))
        }
        return result;
    }


    var getCorner = function () {
        return chart.node.corner.caption;
    }

    var getXAxis = function () {
        return {
            categories: rows
        }
    }

    var yAxis = function () {

    }

    var getSeries = function () {
        var i;
        var result = [];
        for (i = 0; i < columns.length; i++) {
            var column = {
                name: columns[i],
                data: columnData(columns[i])
            };
            result.push(column);
        }
        return result;
    }

    var columnData = function (name) {
        var colIndex = columns.indexOf(name)
//        console.log(name + colIndex)
        var i;
        var result = [];
        for (i = 0; i < rowCount; i++) {
//            console.log(values)
//            console.log(i * columnCount + colIndex)
            result.push(values[i * columnCount + colIndex])
        }
//        console.log(result)
        return result;
    }


    var module = {
        init: function (data) {
            var x2js = new X2JS();
            jsonData = x2js.xml_str2json(data);
            chart = getChart();
            columns = getColumns();
            rows = getRows();
            values = getValues();
            corner = getCorner();
            xAxis = getXAxis();

            series = getSeries();
        },

        series: function () {
            console.log(series);
            return series
        },
        corner: function () {
            return corner
        },
        xAxis: function () {
            console.log(xAxis);
            return xAxis
        },
        setType: function (t) {
            console.log('set type to '+t)
            chartType = t;
        },
        setTitle: function (t) {
            title = t;
        },
        options: function () {
            return {
                chart: {
                    type: chartType
                },
                legend: {
                    layout: 'vertical',
                    align: 'right',
                    verticalAlign: 'middle',
                }
            };
        }
    };

    return module;
})();